package com.example.user.service;


import com.example.user.common.Result;
import com.example.user.entity.AddressManagement;
import com.example.user.entity.Order;
import com.example.user.entity.OrderItem;
import com.example.user.mapper.AddressMapper;
import com.example.user.mapper.CartMapper;
import com.example.user.mapper.OrderMapper;
import com.example.user.request.CartRequest;
import com.example.user.response.CartResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AddressMapper addressMapper;


    public List<CartResponse> cartSelectPage(Integer userId) {

        List<CartResponse> cartResponseList = cartMapper.cartSelectPage(userId);
        return cartResponseList;
    }

    public Result cancelCart(Integer productId) {
        int count = cartMapper.cancelCart(productId);
        if (count == 0) {
            return Result.error("400", "移除失败!");
        }
        return Result.success("移除成功!");
    }

    public Result calculateTotal(List<Integer> itemIds) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Integer id : itemIds) {
            List<CartResponse> itemList = cartMapper.findById(id);
            if (itemList!= null &&!itemList.isEmpty()) {
                CartResponse item = itemList.get(0);
                BigDecimal price = item.getPrice();
                if (price == null) {
                    // 根据业务需求决定如何处理，这里暂时设置为0
                    price = BigDecimal.ZERO;
                }
                int quantity = item.getQuantity();
                if (quantity < 0) {
                    // 根据业务需求决定如何处理，这里暂时设置为0
                    quantity = 0;
                }
                totalPrice = totalPrice.add(price.multiply(BigDecimal.valueOf(quantity)));
            }
        }
        return Result.success(totalPrice.doubleValue());
    }



    public Result createOrder(CartRequest cartRequest) {

        Order order = new Order();
        //插入订单组表
       int count= cartMapper.createOrder(cartRequest);

       //查询订单组表

        OrderItem orderItem=cartMapper.selectOrderItemsById(cartRequest);



       //获取地址
       AddressManagement address= addressMapper.selectAdderssById(cartRequest);

       //生成新的订单对象
        order.setOrderId(orderItem.getOrderId());
        order.setShippingAddress(address.getAddressDetail());
        int quantity = orderItem.getQuantity();
        BigDecimal price = orderItem.getPrice();
        BigDecimal totalAmount = price.multiply(BigDecimal.valueOf(quantity));
        order.setTotalAmount(totalAmount );
        order.setCustomerId(address.getUserId());
        order.setNotes("暂无");

       //插入订单表

        int orderCount=orderMapper.createOrderItems(order);




       return Result.success("生成成功");
    }

    public Result updateProductQuantity(CartRequest cartRequest) {

      int count=  cartMapper.updateProductQuantity(cartRequest);
      if(count==0){
          return Result.error("400","修改失败");
      }
      return Result.success("修改成功");
    }

    //查询订单组表
}
